<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\admin\sky;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class SkyCandle extends BaseModel
{
    use SoftDelete;
    protected $name = 'sky_candle';


    /**
     * 今日大蜡烛
     * @param $date
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function todayCandle($date){
        $list = $this->where(array('date' => $date))->cache(true)->select()->toArray();
        if(empty($list)){
            return [
                'candle' => [],
                'seasonalCandle' => []
            ];
        }
        $candle = [];

        foreach($list as $item){
            switch($item['type']){
                case 1:
                    $candle['candle'][] = array(
                        'area' => $item['area'],
                        'images' => json_decode($item['img'],true)
                    );
                    break;
                case 2:
                    $candle['seasonalCandle'][] = array(
                        'area' => $item['area'],
                        'images' => json_decode($item['img'],true)
                    );
                    break;
                default:
                    break;
            }
        }

        return $candle;
    }


    public function candleData(){
        return $this->belongsToMany("SkyCandleData","SkyCandleDetail","candle_data_id","candle_id");
    }
}